<template>
  <div class="app-container">
    <!-- 搜索筛选操作栏 -->
    <div class="filter-container clearfix">
      <el-button v-waves class="filter-item" @click="dialogVisible = true">
        添加白名单账号
      </el-button>
    </div>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="白名单账号" prop="user" align="center" />
      <el-table-column label="实名认证" prop="name" align="center" />
      <el-table-column label="成交" header-align="center">
        <template slot-scope="{ row }">
          <div>订单：{{ row.order_num }}</div>
          <div>金额：{{ row.balance || "0.00" }}</div>
        </template>
      </el-table-column>
      <el-table-column label="账户余额" align="center" prop="money" />
      <el-table-column align="center" label="注册时间" prop="created_at" />
      <el-table-column label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            size="mini"
            plain
            type="danger"
            @click="handleRemove(row.name, row.id)"
          >
            移出白名单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>

    <!-- 添加 -->
    <el-dialog
      title="添加白名单账号"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      destroy-on-close
      width="40%"
    >
      <add-whiteList @search="searchHandle" />
      <div style="margin-top: 40px; text-align: center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="danger" @click="handleSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMemberList, updateMember } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import addWhiteList from './components/add-whiteList' // secondary package based on el-pagination

export default {
  name: 'MemberWhiteList',
  components: {
    Pagination,
    addWhiteList
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      dialogVisible: false,
      listQuery: {
        page: 1,
        per_page: 20,
        status: 1
      },
      searchData: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clearFilter() {
      this.handleFilter()
    },
    searchHandle(data) {
      this.searchData = data
    },
    handleSubmit() {
      if (!this.searchData || this.searchData.length <= 0) {
        this.$message.warning('请先搜索')
        return false
      }
      this.submitHandle(0)
    },
    submitHandle(index) {
      if (this.searchData.length == index) {
        this.$notify({
          title: '成功',
          message: '操作成功',
          type: 'success',
          duration: 2000
        })
        this.searchData = []
        this.getList()
        return false
      }
      updateMember(this.searchData[index].id, { status: 1 }).then(() => {
        this.dialogVisible = false
        index = index + 1
        this.submitHandle(index)
      })
    },
    handleRemove(name, id) {
      this.$confirm(`您确认要把账号“${name}”移出名单吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          updateMember(id, { status: 2 }).then(() => {
            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          })
        })
        .catch(() => {})
    },
    getList() {
      this.listLoading = true
      getMemberList(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    }
  }
}
</script>
